-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
wasi: add getEnvMap #2390
wasi: add getEnvMap #2390
Conversation
std/os.zig
Outdated
if (env) |ptr| { | ||
var line_i: usize = 0; | ||
while (ptr[line_i] != 0 and ptr[line_i] != '=') : (line_i += 1) {} | ||
if (ptr[line_i] != '=') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be an assertion. If the WASI functions don't do what they're documented to do, that's definitely going to cause undefined behavior. And environ_get
is documented to return the environment string with the format key=value
, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation doesn't say anything. wasmtime returns it in key=value
, wasmer returns argv[0] which is definitely a bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have 2 choices here:
- make the zig code assume the WASI host is correct and do a little bit of prediction about the specifications of the behavior (
key=value
) and allow host bugs to cause bugs in Zig code that uses them. - recognize and work around WASI host bugs & undocumented behavior.
Given that WASI is so brand new, my preference is (1). That means that we recommend Debug or ReleaseSafe builds for now - due to upstream/downstream bugs - and then work with the third party projects to get their bugs fixed / documentation clarified. We can point to the zig standard library and say "this is how we want to implement it, it will work like this if you fix the bug / clarify the docs".
std/os.zig
Outdated
for (environ) |env| { | ||
if (env) |ptr| { | ||
var line_i: usize = 0; | ||
while (ptr[line_i] != 0 and ptr[line_i] != '=') : (line_i += 1) {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with this, but you might consider using std.mem.separate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Combining this and the other comment about assert
, you could do this:
const line = std.mem.toSlice(u8, ptr);
var it = std.mem.separate(line, "=");
const key = it.next().?;
const value = it.next().?;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. This also cleanly handles wasmer.
See: WebAssembly/WASI#27 for the null pointer todo